/*
 * main.c
 *
 *  Created on: Aug 23, 2012
 *      Author: Vasile Guta Ciucur
 *
 *******************************************************************************
 *  BSD License.
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 *  - Neither the name of Vasile Guta Ciucur nor the names of
 *    its contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 *  ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *  POSSIBILITY OF SUCH DAMAGE.
 */

//Enable the following definitions on "atmegaclib.h" header:

//#define UART_BAUD_RATE      57600 // default is 57600
//#define UART_BAUD_SELECT    (F_CPU / (UART_BAUD_RATE * 16L) - 1)
//#define ENABLE_SERIAL_POLL  // require CONVERSION, conflicts with SERIAL
//#define ENABLE_ADC          // analog to digital converter
//#define ENABLE_CONVERSION   // useful for Serial, LCD and 7SEG Display


#ifndef F_CPU
	#define F_CPU 16000000U //required by Atmel Studio 6
#endif
#include <avr/io.h>
#include <util/delay.h>
#include <atmegaclib.h>

uint16_t adcval1, adcval2;

void main(void) __attribute__((noreturn)); //this allows me to have a void main() function
void main (void)
{
	// setting the pins direction...
#if defined(__AVR_ATmega48__)   || \
    defined(__AVR_ATmega88__)      || \
    defined(__AVR_ATmega88P__)     || \
    defined(__AVR_ATmega168__)     || \
    defined(__AVR_ATmega168P__)    || \
    defined(__AVR_ATmega328P__)
	cbi(DDRC, 0);//analog input - 10K pot.
	cbi(DDRC, 1);//analog input - 10K pot.
	sbi(DDRC, 2);//digital output on a LED - to study the effect on analog inputs...
	cbi(PORTC, 2); //output low, so LED is lighted (LED is sinked to the microcontroller)
#elif defined(__AVR_ATmega16__)  || \
    defined(__AVR_ATmega32__)  || \
    defined(__AVR_ATmega164__) || \
    defined(__AVR_ATmega164P__) || \
    defined(__AVR_ATmega164PA__) || \
    defined(__AVR_ATmega324__) || \
    defined(__AVR_ATmega324P__) || \
    defined(__AVR_ATmega324PA__) || \
    defined(__AVR_ATmega644__) || \
    defined(__AVR_ATmega644P__) || \
    defined(__AVR_ATmega644PA__) || \
    defined(__AVR_ATmega1284P__) || \
    defined(__AVR_ATmega1284__) // Sanguino

	cbi(DDRA, 0);//analog input - 10K pot.
	cbi(DDRA, 1);//analog input - 10K pot.
	sbi(DDRA, 2);//digital output on a LED - to study the effect on analog inputs...
	cbi(PORTA, 2); //output 0, so LED is lighted (LED is sinked to the microcontroller)
#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  // I did not tested the ADC for these microcontrollers
  cbi(DDRF, 0);
  cbi(DDRF, 1);
  sbi(DDRF, 2);
  cbi(PORTF, 2);
#endif

	// other initializations
	serial_init();
	adc_init(VREF_AVCC_CAP_AREF, ADC_PRESCALER_128);
	//power off the digital buffers from analog inputs
	adc_poweroff_digital_pinbuffer(0);
	adc_poweroff_digital_pinbuffer(1);
	// --
	for(;;){
		//
		adcval1 = adc_get(0); // read analog input 0
		serial_putstr((uint8_t *)"Potentiometer No.1 = \0");
		serial_putint(adcval1, 10);
		serial_putstr((uint8_t *)"\n\r\0");
#if defined(__AVR_ATmega48__)   || \
    defined(__AVR_ATmega88__)      || \
    defined(__AVR_ATmega88P__)     || \
    defined(__AVR_ATmega168__)     || \
    defined(__AVR_ATmega168P__)    || \
    defined(__AVR_ATmega328P__)
		tbi(PORTC, 2); // toggle LED
#elif defined(__AVR_ATmega16__)  || \
    defined(__AVR_ATmega32__)  || \
    defined(__AVR_ATmega164__) || \
    defined(__AVR_ATmega164P__) || \
    defined(__AVR_ATmega164PA__) || \
    defined(__AVR_ATmega324__) || \
    defined(__AVR_ATmega324P__) || \
    defined(__AVR_ATmega324PA__) || \
    defined(__AVR_ATmega644__) || \
    defined(__AVR_ATmega644P__) || \
    defined(__AVR_ATmega644PA__) || \
    defined(__AVR_ATmega1284P__) || \
    defined(__AVR_ATmega1284__) // Sanguino

		tbi(PORTA, 2); // toggle LED
#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
		// I did not tested the ADC for these microcontrollers
		tbi(PORTF, 2);
#endif
		_delay_ms(250);
		adcval2 = adc_get(1);
		serial_putstr((uint8_t *)"Potentiometer No.2 = \0");
		serial_putint(adcval2, 10);
		serial_putstr((uint8_t *)"\n\r\0");
	}
}
